using System;
using CommandSystem;
using Exiled.API.Features;

namespace RgCore.CustomSide.Commands.SideManagerCommands
{
    public class Set : ICommand
    {
        public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out string response)
        {
            if (!sender.CheckPermission(PlayerPermissions.ForceclassWithoutRestrictions))
            {
                response = $"权限不足,需要{nameof(PlayerPermissions.ForceclassWithoutRestrictions)}权限";
                return false;
            }

            if (arguments.Count < 1)
            {
                response = "用法：sidem set <玩家ID> [阵营ID（不填则清空）]";
                return false;
            }

            if (!int.TryParse(arguments.At(0), out var id))
            {
                response = "请输入一个合法的数字！";
                return false;
            }

            if (!Player.TryGet(id, out var tar))
            {
                response = "玩家不存在！请检查ID是否存在！";
                return false;
            }

            if (arguments.Count >= 2)
            {
                CustomSide tar_side = arguments.At(1).GetSideById();
                if (tar_side == null)
                {
                    response = "阵营不存在！";
                    return false;
                }

                SideManager.CustomedSide[tar] = tar_side;
                response = "已将玩家分配至：" + $"<color={tar_side.SideColor}>{tar_side.SideName}</color>";
                return true;
            }

            SideManager.CustomedSide.Remove(tar);
            response = "已移除玩家的自定义阵营";
            return true;
        }

        public string Command { get; } = "set";
        public string[] Aliases { get; } = Array.Empty<string>();
        public string Description { get; } = "设置或清空玩家的阵营";
    }
}